import {
    reqcatelist,
    twoGoods,
    seckAdd,
    seckList,
    seckDel,
} from "../../request/api"
const state = {
    //初始值
    cateList1: [],
    cateList2: [],
    twoGoods: [],
    seckillList: [],
}

const getters = {
    //导出给组件
    cateList1(state) {
        return state.cateList1
    },
    cateList2(state) {
        return state.cateList2
    },
    twoGoods(state) {
        return state.twoGoods
    },
    seckillList(state) {
        return state.seckillList
    },
}

const mutations = {
    //修改list
    changeCateList1(state, list) {
        state.cateList1 = list;
    },
    changeCateList2(state, list) {
        state.cateList2 = list;
    },
    changeTwoGoods(state, list) {
        state.twoGoods = list;
    },
    changeSeckillList(state, list) {
        state.seckillList = list;
    },
}

const actions = {
    reqcatelist1(context, pid) {
        if (pid) {
            reqcatelist({
                pid: pid
            }).then(res => {
                context.commit("changeCateList1", res.data.list)
            })
        } else {
            reqcatelist({
                istree: true
            }).then(res => {
                context.commit("changeCateList1", res.data.list)
            })
        }
    },
    reqcatelist2(context, pid) {
        if (pid) {
            reqcatelist({
                pid: pid
            }).then(res => {
                context.commit("changeCateList2", res.data.list)
            })
        } else {
            reqcatelist({
                istree: true
            }).then(res => {
                context.commit("changeCateList2", res.data.list)
            })
        }
    },
    reqTwoGoods(context, cateid) {
        twoGoods({
            cateid: cateid,
            type:2
        }).then(res => {
            context.commit("changeTwoGoods", res.data.list.goodData)
        })
    },
    seckAdd(context,form){
        seckAdd(form)
    },
    reqSeckList(context){
        seckList().then(res=>{
            if(res.data.code===200){
                context.commit("changeSeckillList",res.data.list)
            }
        })
    },
    reqSeckDel(context,id){
        seckDel({id:id}).then(res=>{
            if(res.data.code===200){
                context.dispatch("reqSeckList")
            }
        })
    },
}


export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}